package self.zhangkang.chart.view.avg.simlpe

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import self.zhangkang.chart.data.KLineInfo
import self.zhangkang.chart.tools.CanvasTools
import self.zhangkang.chart.tools.DateTools
import self.zhangkang.chart.tools.dp2px
import self.zhangkang.chart.view.pool.PathPool
import self.zhangkang.chart.view.pool.RectPool
import self.zhangkang.chart.view.touch.TouchHelper
import kotlin.math.floor

/**
 * @author : Android-张康
 * created on: 2023/5/4 10:59
 * description: 简单分时图
 */
class SimpleAvgChartView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr), TouchHelper.OnListener {

    companion object {

        /**
         * 默认展示K线数量
         */
        private const val DEFAULT_AVG_LINE_NUMBER: Int = 240

        /**
         * 单根K线默认宽度（打开时）：6 单位 dp
         */
        private const val K_LINE_DEFAULT_WIDTH: Int = 6

        /**
         * 单根K线最大宽度：20 单位 dp
         */
        private const val CANDLESTICK_MAX_WIDTH: Int = 20

        /**
         * 单根K线最小宽度：1 单位 dp
         */
        private const val CANDLESTICK_MIN_WIDTH: Int = 1

        /**
         * K线之间的间距：0 单位 dp
         */
        private const val CANDLESTICK_SPACE: Int = 0
    }

    private val mKLineData = mutableListOf<KLineInfo>()

    /**
     * 蜡烛默认展示的数量
     */
    private var mShowCandlestickNumber = DEFAULT_AVG_LINE_NUMBER

    /**
     * 蜡烛的最大宽度
     */
    private val candlestickMaxWidth = CANDLESTICK_MAX_WIDTH.dp2px(context)

    /**
     * 蜡烛的最小计算宽度
     */
    internal val candlestickCalculationMinWidth = CANDLESTICK_MIN_WIDTH.dp2px(context)

    /**
     * 蜡烛的最小宽度
     */
    private val candlestickMinWidth = candlestickCalculationMinWidth / 4 * 3

    /**
     * 单个K线绘制宽度
     */
    internal var mCandlestickWidth = K_LINE_DEFAULT_WIDTH.dp2px(context)

    /**
     * 蜡烛之间默认的距离
     */
    private val candlestickDefaultSpace = CANDLESTICK_SPACE

    /**
     * K线之间的距离
     */
    private var mCandlestickSpace = candlestickDefaultSpace

    /**
     * 阳线是否为实心
     */
    private var mYangLineIsFill = true

    /**
     * 是否获取到了宽
     */
    private var mIsGetWidth = false

    /**
     * 记录数据开始的下标
     */
    private var mStartDataIndex = 0

    /**
     * 记录数据结束的下标
     */
    private var mEndDataIndex = 0

    private val mSimpleAvgViewColor = SimpleAvgViewColor(context)

    private val mSimpleAvgViewDimen = SimpleAvgViewDimen(context)

    /**
     * 线的画笔
     */
    private val mLinePaint: Paint = Paint().apply {
        style = Paint.Style.STROKE
        isAntiAlias = true
        isDither = true
    }

    /**
     * 文字的画笔
     */
    private val mTextPaint = Paint().apply {
        isAntiAlias = true
        isDither = true
    }

    /**
     * K线是否为一条线
     */
    private var mKLineIsLine = false

    /**
     * 缓存Path
     */
    private val mPathPool = PathPool()

    /**
     * 缓存 Rect
     */
    private val mRectPool = RectPool()

    /**
     * 触摸协助类
     */
    private val mTouchHelper: TouchHelper = TouchHelper(context, this, this, false)

    private val mIndexInfoHelper = SimpleAvgIndexInfoHelper(
        pathPool = mPathPool,
        rectPool = mRectPool,
        simpleAvgViewColor = mSimpleAvgViewColor,
        simpleAvgViewDimen = mSimpleAvgViewDimen,
    )

    private val mAvgChartIndexList = mutableListOf(
        mIndexInfoHelper.createKLineIndexInfo(SimpleAvgChartIndex.AvgChart),
        mIndexInfoHelper.createKLineIndexInfo(SimpleAvgChartIndex.AvgTurnVolume),
    )

    /**
     * 副图的高度
     */
    private var mSubplotHeight = 0.0F

    /**
     * 记录当前触摸的数据下标
     */
    private var mCurrentDataIndex = -1

    /**
     * 记录当前触摸的 X 坐标
     */
    private var mLongPressX = -1F

    /**
     * 记录当前触摸的 Y 坐标
     */
    private var mLongPressY = -1F

    /**
     * 记录最后触摸的指标
     */
    private var mLastTouchIndex = -1

    init {
        post {
            calculationDrawNumber()
            calculationMainAndSubplotHeight()
        }
    }

    override fun computeScroll() {
        super.computeScroll()
        mTouchHelper.computeScrollOffset()
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return mTouchHelper.onTouchEvent(event)
    }

    override fun onDraw(canvas: Canvas) {
        if (!mIsGetWidth) {
            return
        }
        val paddingStart = paddingStart.toFloat()
        val paddingEnd = paddingEnd.toFloat()
        val paddingTop = paddingTop.toFloat()
        val paddingBottom = paddingBottom.toFloat()
        val startX = paddingStart
        val stopX = width - paddingEnd
        val startY = paddingTop
        val stopY = height - paddingBottom

        //绘制指标图
        val subplotHeight = mSubplotHeight
        var subplotStartY = startY
        var subplotStopY: Float
        val lineHeight = mSimpleAvgViewDimen.borderLineStrokeWidth
        var isFirst = true
        mAvgChartIndexList.forEach {
            val canvasStartY: Float
            if (isFirst) {
                canvasStartY = subplotStartY + mSimpleAvgViewDimen.mainTopSpace
                subplotStopY =
                    canvasStartY + it.getProportion() * subplotHeight + mSimpleAvgViewDimen.dateSpace
            } else {
                canvasStartY = subplotStartY + mSimpleAvgViewDimen.subplotSpace
                subplotStopY = canvasStartY + it.getProportion() * subplotHeight
            }
            //先绘制背景
            it.drawBackground(
                canvas = canvas,
                startX = startX,
                startY = canvasStartY,
                stopX = stopX,
                stopY = subplotStopY,
                textPaint = mTextPaint,
                linePaint = mLinePaint.apply {
                    strokeWidth = mSimpleAvgViewDimen.borderLineStrokeWidth
                    color = mSimpleAvgViewColor.borderLineColor
                }
            )
            subplotStartY = subplotStopY
            isFirst = false
        }
        if (mKLineData.isEmpty()) {
            return
        }
        val currentIndex = if (mCurrentDataIndex < 0) {
            mEndDataIndex
        } else {
            mCurrentDataIndex
        }
        //绘制指标信息
        val currentKLineInfo: KLineInfo = mKLineData[currentIndex]
        subplotStartY = startY
        subplotStopY = subplotStartY
        isFirst = true
        mAvgChartIndexList.forEach {
            val canvasStartY: Float
            if (isFirst) {
                canvasStartY = subplotStartY
                subplotStopY = subplotStartY + mSimpleAvgViewDimen.mainTopSpace
            } else {
                canvasStartY = subplotStartY
                subplotStopY = subplotStartY + mSimpleAvgViewDimen.subplotSpace
            }
            //绘制当前指标的值
            it.drawCurrentIndexInfo(
                canvas = canvas,
                startX = startX,
                startY = canvasStartY + mSimpleAvgViewDimen.borderLineStrokeWidth,
                stopX = stopX,
                stopY = subplotStopY - mSimpleAvgViewDimen.borderLineStrokeWidth,
                marginTop = lineHeight,
                span = 3,
                indexValueHorizontalSpace = mSimpleAvgViewDimen.valueHorizontalSpace,
                indexValueVerticalSpace = mSimpleAvgViewDimen.valueVerticalSpace,
                kLineInfo = currentKLineInfo,
                textPaint = mTextPaint,
                linePaint = mLinePaint,
                kLineIsLine = mKLineIsLine
            )
            subplotStartY = if (isFirst) {
                subplotStopY + it.getProportion() * subplotHeight + mSimpleAvgViewDimen.dateSpace
            } else {
                subplotStopY + it.getProportion() * subplotHeight
            }
            isFirst = false
        }
        subplotStartY = startY
        subplotStopY = subplotStartY
        isFirst = true
        mAvgChartIndexList.forEach {
            val canvasStartY: Float
            if (isFirst) {
                canvasStartY = subplotStartY + mSimpleAvgViewDimen.mainTopSpace
                subplotStopY =
                    canvasStartY + it.getProportion() * subplotHeight + mSimpleAvgViewDimen.dateSpace
            } else {
                canvasStartY = subplotStartY + mSimpleAvgViewDimen.subplotSpace
                subplotStopY = canvasStartY + it.getProportion() * subplotHeight
            }
            it.beforeDrawItemContent(
                canvas = canvas,
                startX = startX,
                startY = canvasStartY,
                stopX = stopX,
                stopY = subplotStopY,
                paint = mLinePaint,
                candlestickWidth = mCandlestickWidth,
                candlestickWidthSpace = mCandlestickSpace.toFloat(),
                kLineInfo = currentKLineInfo,
                kLineIsLine = mKLineIsLine
            )
            subplotStartY = subplotStopY
            isFirst = false
        }
        //蜡烛图的宽度
        val candlestickWidth: Float = if (mKLineIsLine) {
            candlestickCalculationMinWidth
        } else {
            mCandlestickWidth
        }
        val startIndex = mStartDataIndex
        val endIndex = mEndDataIndex
        val drawWidth = stopX - startX
        val showKLineNumber = mShowCandlestickNumber
        val dataNumber = endIndex - startIndex + 1
        val drawNumber = Math.min(dataNumber, showKLineNumber)
        //drawNumber == showKLineNumber 说明当前可以绘制满整个屏幕，第一次位移到最右方即可。
        //否则就说明当前蜡烛的数量不足以绘制满整个屏幕，位移（蜡烛图宽度+空隙）*(绘制数量-1)+边框线的宽度
        var totalTranslateX = if (drawNumber == showKLineNumber) {
            drawWidth - mSimpleAvgViewDimen.borderLineStrokeWidth - candlestickWidth
        } else {
            (candlestickWidth + mCandlestickSpace) * (drawNumber - 1) + mSimpleAvgViewDimen.borderLineStrokeWidth
        }
        val halfCandlestickWidth = candlestickWidth / 2
        val translateX = -mCandlestickSpace - candlestickWidth
        canvas.save()
        canvas.translate(totalTranslateX, 0F)
        var isFirstData = true
        for (index in endIndex downTo startIndex) {
            val kLineInfo = mKLineData[index]
            subplotStartY = startY
            subplotStopY = subplotStartY
            isFirst = true
            mAvgChartIndexList.forEach {
                val canvasStartY: Float
                if (isFirst) {
                    canvasStartY = subplotStartY + mSimpleAvgViewDimen.mainTopSpace
                    subplotStopY =
                        canvasStartY + it.getProportion() * subplotHeight + mSimpleAvgViewDimen.dateSpace
                } else {
                    canvasStartY = subplotStartY + mSimpleAvgViewDimen.subplotSpace
                    subplotStopY = canvasStartY + it.getProportion() * subplotHeight
                }
                it.drawItemContent(
                    canvas = canvas,
                    startX = startX,
                    startY = canvasStartY,
                    stopX = stopX,
                    stopY = subplotStopY,
                    textPaint = mTextPaint,
                    linePaint = mLinePaint,
                    kLineInfo = kLineInfo,
                    isFirst = isFirstData,
                    yangLineIsFill = mYangLineIsFill,
                    totalTranslateX = totalTranslateX,
                    candlestickWidth = candlestickWidth,
                    halfCandlestickWidth = halfCandlestickWidth,
                    kLineIsLine = mKLineIsLine
                )
                subplotStartY = subplotStopY
                isFirst = false
            }
            canvas.translate(translateX, 0F)
            totalTranslateX += translateX
            isFirstData = false
        }
        canvas.restore()
        mAvgChartIndexList.forEach {
            it.afterDrawItemContent(canvas, mLinePaint.apply {
                style = Paint.Style.STROKE
                strokeWidth = mSimpleAvgViewDimen.borderLineStrokeWidth
            })
        }
        if (mLongPressX in startX..stopX && mLongPressY in startY..stopY) {
            mLinePaint.color = mSimpleAvgViewColor.highlightLineColor
            mLinePaint.strokeWidth = mSimpleAvgViewDimen.highlightLineWidth
            canvas.drawLine(startX, mLongPressY, stopX, mLongPressY, mLinePaint)
            canvas.drawLine(
                mLongPressX,
                startY + mSimpleAvgViewDimen.mainTopSpace,
                mLongPressX,
                stopY,
                mLinePaint
            )
            findAvgIndexInfo()?.let {
                val leftText = it.first.getCurrentLeftValue(it.second)
                if (!leftText.isNullOrEmpty()) {
                    CanvasTools.drawHighlightLeftOrRightValue(
                        canvas = canvas,
                        startX = startX,
                        stopX = stopX,
                        startY = startY,
                        stopY = stopY,
                        longPressX = mLongPressX,
                        longPressY = mLongPressY,
                        textStartY = startY + mSimpleAvgViewDimen.mainTopSpace + subplotHeight * mAvgChartIndexList.first()
                            .getProportion() + mSimpleAvgViewDimen.maxOrMinPriceTextPaddingLine,
                        rectPool = mRectPool,
                        drawText = leftText,
                        textPaint = mTextPaint,
                        backgroundColor = mSimpleAvgViewColor.highlightLineBgColor,
                        textSize = mSimpleAvgViewDimen.ordinateTextSize,
                        textColor = mSimpleAvgViewColor.highlightLineTextColor,
                        isLeft = true
                    )
                }
                val rightText = it.first.getCurrentRightValue(it.second)
                if (!rightText.isNullOrEmpty()) {
                    CanvasTools.drawHighlightLeftOrRightValue(
                        canvas = canvas,
                        startX = startX,
                        stopX = stopX,
                        startY = startY,
                        stopY = stopY,
                        longPressX = mLongPressX,
                        longPressY = mLongPressY,
                        textStartY = startY + mSimpleAvgViewDimen.mainTopSpace + subplotHeight * mAvgChartIndexList.first()
                            .getProportion(),
                        rectPool = mRectPool,
                        drawText = rightText,
                        textPaint = mTextPaint,
                        backgroundColor = mSimpleAvgViewColor.highlightLineBgColor,
                        textSize = mSimpleAvgViewDimen.ordinateTextSize,
                        textColor = mSimpleAvgViewColor.highlightLineTextColor,
                        isLeft = false
                    )
                }
            }

            val bottomText = DateTools.toyyyy_MM_dd_HH_mm(currentKLineInfo.getDataTime())
            CanvasTools.drawHighlightBottomValue(
                canvas = canvas,
                startX = startX,
                stopX = stopX,
                startY = startY,
                stopY = stopY,
                longPressX = mLongPressX,
                textStartY = startY + mSimpleAvgViewDimen.mainTopSpace + subplotHeight * mAvgChartIndexList.first()
                    .getProportion(),
                rectPool = mRectPool,
                drawText = bottomText,
                textPaint = mTextPaint,
                backgroundColor = mSimpleAvgViewColor.highlightLineBgColor,
                textSize = mSimpleAvgViewDimen.ordinateTextSize,
                textColor = mSimpleAvgViewColor.highlightLineTextColor,
                textPaddingHorizontal = mSimpleAvgViewDimen.textPaddingLine
            )
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mTouchHelper.onDetachedFromWindow()
    }

    fun setKLineData(klineData: List<KLineInfo>) {
        mKLineData.clear()
        mKLineData.addAll(klineData)
        calculation()
        invalidate()
    }

    /**
     * 计算数据
     */
    private fun calculation() {
        calculationMainAndSubplotHeight()
        //计算绘制范围
        calculationDrawRangeByScale()
        //计算最大值和最小值
        calculationMaxAndMin()
    }

    /**
     * 缩放时，保持结束坐标不变，仅修改开始坐标
     */
    private fun calculationDrawRangeByScale() {
        val oldEndIndex = mEndDataIndex
        val showCandlestickNumber = mShowCandlestickNumber
        //计算绘制范围
        var startIndex: Int
        var endIndex: Int
        if (mKLineData.size > showCandlestickNumber) {
            endIndex = if (oldEndIndex == 0) {
                mKLineData.size - 1
            } else {
                oldEndIndex
            }
            startIndex = endIndex - showCandlestickNumber + 1
            if (startIndex < 0) {
                startIndex = 0
                endIndex = showCandlestickNumber
            }
        } else {
            startIndex = 0
            endIndex = mKLineData.size - 1
        }
        mStartDataIndex = startIndex
        mEndDataIndex = endIndex
    }

    /**
     * 计算主副图的高度
     */
    private fun calculationMainAndSubplotHeight() {
        val drawHeight = height - paddingTop - paddingBottom
        var sum = 0
        mAvgChartIndexList.forEach {
            sum += it.getProportion()
        }
        //计算的高度 = 绘制的高度 - 与副图之间的高度 * 副图数量 - 顶部的高度 - 时间的高度
        var calculationHeight =
            drawHeight - mSimpleAvgViewDimen.subplotSpace * (mAvgChartIndexList.size - 1) * 1.0F - mSimpleAvgViewDimen.mainTopSpace - mSimpleAvgViewDimen.dateSpace
        calculationHeight /= sum
        val subplotHeight = calculationHeight
        mSubplotHeight = subplotHeight
    }

    /**
     * 计算最大值和最小值
     */
    private fun calculationMaxAndMin() {
        if (mKLineData.isEmpty()) {
            return
        }
        val startIndex: Int = mStartDataIndex
        val endIndex: Int = mEndDataIndex
        val startKLine = mKLineData[startIndex]
        mAvgChartIndexList.forEach {
            it.calculationMaxAndMinBefore(startKLine)
        }
        for (index in startIndex..endIndex) {
            mAvgChartIndexList.forEach {
                it.calculationMaxAndMin(mKLineData[index])
            }
        }
        mAvgChartIndexList.forEach {
            it.calculationMaxAndMinAfter()
        }
    }

    /**
     * 计算绘制的数量
     *
     * 根据当前屏幕的宽度和蜡烛图的宽度计算
     *
     */
    private fun calculationDrawNumber() {
        val drawWidth =
            width - paddingStart - paddingEnd - 2 * mSimpleAvgViewDimen.borderLineStrokeWidth
        mCandlestickWidth = drawWidth / DEFAULT_AVG_LINE_NUMBER
        mIsGetWidth = true
        invalidate()
    }

    override fun onScale(scaleFactor: Float) {
        mCandlestickWidth *= scaleFactor
        mKLineIsLine = false
        if (mCandlestickWidth > candlestickMaxWidth) {
            mCandlestickWidth = candlestickMaxWidth
        } else if (mCandlestickWidth < candlestickCalculationMinWidth) {
            if (mCandlestickWidth < candlestickMinWidth) {
                mCandlestickWidth = candlestickMinWidth
            }
            mKLineIsLine = true
        }
        //计算绘制的数量
        calculationDrawNumber()
        //计算绘制范围
        calculationDrawRangeByScale()
        //计算最大值和最小值
        calculationMaxAndMin()
        invalidate()
    }

    /**
     * 滚动页面
     *
     * @param scrollX 本次滚动的X
     */
    override fun onScroll(scrollX: Float) {
        var startIndex = mStartDataIndex
        val maxEnd = mKLineData.size - 1
        //增加灵敏度
        val scrollNumber = (if (scrollX >= 0) {
            scrollX / mCandlestickWidth + 0.75F
        } else {
            scrollX / mCandlestickWidth - 0.75F
        }).toInt()
        startIndex += scrollNumber
        if (startIndex < 0) {
            startIndex = 0
        }
        var endIndex = startIndex + mShowCandlestickNumber
        if (endIndex > maxEnd) {
            endIndex = maxEnd
        }
        //修正开始的位置
        startIndex = endIndex - mShowCandlestickNumber
        if (startIndex < 0) {
            startIndex = 0
        }
        //如果已经滑动到头，停止滑动
        if (startIndex == 0 || endIndex == mKLineData.size - 1) {
            mTouchHelper.forceFinished(true)
        }
        mStartDataIndex = startIndex
        mEndDataIndex = endIndex
        calculationMaxAndMin()
        invalidate()
    }

    override fun onSingleTapUp(touchX: Float, touchY: Float) {
        val startY = paddingTop
        var subplotStartY = startY.toFloat()
        var subplotStopY: Float
        val subplotHeight = mSubplotHeight
        mAvgChartIndexList.forEachIndexed { index, kLineIndexInfo ->
            subplotStopY = subplotStartY + kLineIndexInfo.getProportion() * subplotHeight
            if (touchY > subplotStartY && touchY < subplotStopY) {
                if (!mIndexInfoHelper.canSwitch(kLineIndexInfo)) {
                    return
                }
                mAvgChartIndexList[index] = mIndexInfoHelper.nextIndex(kLineIndexInfo)
                calculationMaxAndMin()
                invalidate()
                return
            }
            subplotStartY = subplotStopY
        }
    }

    override fun onLongPress(touchX: Float, touchY: Float, isFinish: Boolean) {
        if (isFinish) {
            mLongPressX = -1F
            mLongPressY = -1F
            mCurrentDataIndex = -1
            mLastTouchIndex = -1
            invalidate()
            return
        }
        val candlestickSpace = mCandlestickSpace
        val candlestickWidth = if (mKLineIsLine) {
            candlestickCalculationMinWidth
        } else {
            mCandlestickWidth
        }
        val startIndex = mStartDataIndex
        val endIndex = mEndDataIndex

        //真正触摸在控件内的 X 坐标
        val startX = paddingStart + mSimpleAvgViewDimen.borderLineStrokeWidth
        val stopX = width - paddingEnd
        var realTouchX = touchX
        if (touchX < startX) {
            realTouchX = startX
        }
        if (realTouchX > stopX) {
            realTouchX = stopX.toFloat()
        }
        //向下取整，获取当前触摸到屏幕内的位置
        val index = floor((realTouchX - startX) / (candlestickWidth + candlestickSpace)).toInt()
        //开始位置+屏幕中的位置=真正触摸的数据下标
        var touchIndex = startIndex + index
        if (touchIndex >= endIndex) {
            touchIndex = endIndex
            //修正触摸的X
            mLongPressX =
                startX + (endIndex - startIndex + 1) * (candlestickWidth + candlestickSpace) - candlestickSpace
        } else {
            mLongPressX = realTouchX
        }
        mCurrentDataIndex = touchIndex

        //修正触摸的Y
        val startY = paddingTop + mSimpleAvgViewDimen.mainTopSpace
        val stopY = height - paddingBottom
        var realTouchY = touchY
        if (realTouchY < startY) {
            realTouchY = startY
        }
        if (realTouchY > stopY) {
            realTouchY = stopY.toFloat()
        }
        var subplotStartY = startY
        var subplotStopY: Float
        val subplotHeight = mSubplotHeight
        var isFirst = true
        kotlin.run ForEach@{
            mAvgChartIndexList.forEachIndexed { index, simpleAvgIndexInfo ->
                val canvasStartY: Float = subplotStartY
                subplotStopY = canvasStartY + simpleAvgIndexInfo.getProportion() * subplotHeight
                if (realTouchY in canvasStartY..subplotStopY) {
                    mLongPressY = realTouchY
                    mLastTouchIndex = index
                    return@ForEach
                }
                val nextStartY = if (isFirst) {
                    subplotStopY + mSimpleAvgViewDimen.dateSpace + mSimpleAvgViewDimen.subplotSpace
                } else {
                    subplotStopY + mSimpleAvgViewDimen.subplotSpace
                }
                if (realTouchY in subplotStopY..nextStartY) {
                    realTouchY = if (mLastTouchIndex == index) {
                        subplotStopY
                    } else {
                        nextStartY
                    }
                    mLongPressY = realTouchY
                    return@ForEach
                }
                subplotStartY = nextStartY
                isFirst = false
            }
        }
        invalidate()
    }

    override fun getCandlestickWidth(): Float {
        return mCandlestickWidth
    }

    private fun findAvgIndexInfo(): Pair<SimpleAvgIndexInfo, Float>? {
        val longPressX = mLongPressX
        val longPressY = mLongPressY
        if (longPressX > 0 && longPressY > 0) {
            val startY = paddingTop + mSimpleAvgViewDimen.mainTopSpace
            var subplotStartY = startY
            var indexStopY: Float
            val subplotHeight = mSubplotHeight
            var isFirst = true
            mAvgChartIndexList.forEach {
                val indexStartY: Float = subplotStartY
                indexStopY = indexStartY + it.getProportion() * subplotHeight
                if (longPressY in indexStartY..indexStopY) {
                    return Pair(it, 1 - (longPressY - indexStartY) / (indexStopY - indexStartY))
                }
                subplotStartY = if (isFirst) {
                    indexStopY + mSimpleAvgViewDimen.dateSpace + mSimpleAvgViewDimen.subplotSpace
                } else {
                    indexStopY + mSimpleAvgViewDimen.subplotSpace
                }
                isFirst = false
            }
        }
        return null
    }
}